package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class _zhaoqingNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://zwgk.zhaoqing.gov.cn/zq310/[0-9]*/t[0-9]*_[0-9]*.html",
		"http://www.zhaoqing.gov.cn/jjzq2011/xwdt/zqyw/[0-9]*/t[0-9]*_[0-9]*.html",
		"http://www.zhaoqing.gov.cn/jjzq2011/gg/[0-9]*/t[0-9]*_[0-9]*.html",
		"http://www.gd.gov.cn/govpub/gsgg/[0-9]*/t[0-9]*_[0-9]*.htm",
		"http://www.zhaoqing.gov.cn/[a-z]*/[a-z]*/[0-9]*/.*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt=urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		/*
		 * 标题
		 */
		title=doc.select("h1.title_pos").text();

		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
		}

		/*
		 * 内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));
        if(StringUtils.isBlank(content)){
        	content=doc.select("div.content").text();
        }
		/*
		 * 时间
		 */
		date=DateUtils.matchDate(doc.select("span.f-l").text());
		news.setUrl(url);
		String author= JSoupUtils.matchAuthor(doc, "来源:");
		/*
		 * 来源
		 */
		news.setAuthor(author);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		_zhaoqingNewsAnalyse analyse=new _zhaoqingNewsAnalyse();
		String url="http://www.zhaoqing.gov.cn/xwzx/zqyw/201611/t20161107_413118.html";
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(analyse.isDetailPage(url));
		System.err.println(url);
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}


	
	public boolean isNeedUpdate(){
		return false;
	}

}
